package edu.pku.sei.act.classGenerator;

import edu.pku.sei.act.taskStruct.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

 public class InitTransitionClassGenerator {  
  protected static String nl;
  public static synchronized InitTransitionClassGenerator create(String lineSeparator)
  {
    nl = lineSeparator;
    InitTransitionClassGenerator result = new InitTransitionClassGenerator();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "";
  protected final String TEXT_2 = NL + NL + "package edu.pku.sei.act.";
  protected final String TEXT_3 = ";" + NL + "" + NL + "" + NL + "import edu.pku.sei.act.actools.util.AbilityOutput;" + NL + "import edu.pku.sei.act.actools.util.TaskMessage;";
  protected final String TEXT_4 = "\t" + NL + "import edu.pku.sei.act.";
  protected final String TEXT_5 = ".";
  protected final String TEXT_6 = ".";
  protected final String TEXT_7 = ";";
  protected final String TEXT_8 = NL + "import android.content.BroadcastReceiver;" + NL + "import android.content.Context;" + NL + "import android.content.Intent;" + NL + "import android.os.RemoteException;" + NL + "import android.util.Log;" + NL + "" + NL + "public class ";
  protected final String TEXT_9 = " extends BroadcastReceiver {" + NL + "" + NL + "\t@Override" + NL + "\tpublic void onReceive(Context context, Intent intent) {" + NL + "\t\tLog.v(\"@jjj\", \"";
  protected final String TEXT_10 = "!\");" + NL + "\t\tString rs = (String) intent.getExtras().get(\"arg1\");" + NL + "\t\tTaskMessage msg = new TaskMessage(rs);" + NL + "\t\t" + NL + "\t\t";
  protected final String TEXT_11 = "\t\t" + NL + "\t\tif (msg.getContent().equals(\"";
  protected final String TEXT_12 = "\")) {" + NL + "\t\t\ttry {" + NL + "\t\t\t" + NL + "\t\t\t";
  protected final String TEXT_13 = NL + "\t\t\t\t";
  protected final String TEXT_14 = ".capability.";
  protected final String TEXT_15 = " = new AbilityOutput(";
  protected final String TEXT_16 = ".getCm().invokeCapability(";
  protected final String TEXT_17 = ".capability.";
  protected final String TEXT_18 = ", null));" + NL + "\t\t\t\t" + NL + "\t\t\t";
  protected final String TEXT_19 = NL + "\t\t\t\t\t\t\t" + NL + "\t\t\t\tAutonomicParking.setState(";
  protected final String TEXT_20 = ".state.";
  protected final String TEXT_21 = ");" + NL + "\t\t\t} catch (RemoteException e1) {" + NL + "\t\t\t\te1.printStackTrace();" + NL + "\t\t\t}" + NL + "\t\t}" + NL + "\t\t" + NL + "\t";
  protected final String TEXT_22 = "\t\t" + NL + "\t}" + NL + "" + NL + "}";
  protected final String TEXT_23 = NL;

 public String generate(Task task)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
    
String taskName = NameGenerator.genTaskName(task.getName());
String taskProcessorName = NameGenerator.genTaskProcessorName(task.getName());
Collection<Role> roles = task.getRoles();

    stringBuffer.append(TEXT_2);
    stringBuffer.append( NameGenerator.genTaskPackageName(task.getName()));
    stringBuffer.append(TEXT_3);
     for(Iterator<Role> it = roles.iterator(); it.hasNext(); ) 
	{
		Role role = it.next();

    stringBuffer.append(TEXT_4);
    stringBuffer.append( NameGenerator.genTaskPackageName(task.getName()));
    stringBuffer.append(TEXT_5);
    stringBuffer.append( NameGenerator.genRolePackageName(role.getName()));
    stringBuffer.append(TEXT_6);
    stringBuffer.append(NameGenerator.genRoleName(role.getName()));
    stringBuffer.append(TEXT_7);
    	}
 
    stringBuffer.append(TEXT_8);
    stringBuffer.append(NameGenerator.genInitTransitionName(task.getInitTrans().getName()));
    stringBuffer.append(TEXT_9);
    stringBuffer.append( NameGenerator.genInitTransitionName(task.getInitTrans().getName()));
    stringBuffer.append(TEXT_10);
    
		InitTransition initTrans = task.getInitTrans();
		Set<String> roleIds = initTrans.getPostRoleIds();
		for(Iterator<String> it = roleIds.iterator(); it.hasNext(); )
		{
			String roleId = it.next();
			Role role = task.getRoleById(roleId);
			String roleName = NameGenerator.genRoleName(role.getName());			
			String stateId = initTrans.getPostStateIdById(roleId);
			State state = role.getStateById(stateId);
			String stateName = NameGenerator.genStateName(state.getName());	
		
    stringBuffer.append(TEXT_11);
    stringBuffer.append( roleName);
    stringBuffer.append(TEXT_12);
     
			for(Iterator<Capability> it2 = role.getCapabilities().iterator(); it2.hasNext(); )
			{
				Capability cap = it2.next();
				String capName = NameGenerator.genCapabilityName(cap.getName());
				String capDescName = NameGenerator.genCapabilityDescriptionName(cap.getName());
				String outputName = NameGenerator.genAbilityOutputName(cap.getName());
				if(NameGenerator.DEAMON.equals(NameGenerator.genCapabilityTypeName(cap.getType())))
				{
			
    stringBuffer.append(TEXT_13);
    stringBuffer.append( roleName);
    stringBuffer.append(TEXT_14);
    stringBuffer.append( capName);
    stringBuffer.append(TEXT_15);
    stringBuffer.append( NameGenerator.genTaskProcessorName(task.getName()));
    stringBuffer.append(TEXT_16);
    stringBuffer.append( roleName);
    stringBuffer.append(TEXT_17);
    stringBuffer.append(capDescName);
    stringBuffer.append(TEXT_18);
      }
			} 
			
    stringBuffer.append(TEXT_19);
    stringBuffer.append( roleName);
    stringBuffer.append(TEXT_20);
    stringBuffer.append( stateName);
    stringBuffer.append(TEXT_21);
     } 
    stringBuffer.append(TEXT_22);
    stringBuffer.append(TEXT_23);
    return stringBuffer.toString();
  }
}